<?php
	/** 注册功能类 
	@author ffpan
	@date 2010-04-06
	@version 1.0
	**/

	class Agent{
		function __construct(){
			$this->fangDb = new wbDatabase();
			$this->order = array(
				1 => " order by lastlogin desc", //登录日期倒序
				2 => " order by name asc", //拼音排名
				3 => " order by number asc", //编号排名
				4 => " order by scores desc" //积分排名
			);
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
	
		//选出品牌中介
		function getGoldenList(){
			$sql = "select id ,`name` , stars , scores , image , lastlogin , number from ".__table_agentlist__." where level = 1 ".$this->order[$type] ." limit ".__index_goldagent_num__;
			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
				   	$row["lastlogin"] = $this->getshowtime($row["lastlogin"]);
					$row["stars"] = $this->getStarByScore($row["scores"]);
					$row["number"] = $this->getShowNumber($row["number"]);
				   $arr[] = $row;
			   }
			   $result->close();
			}
			return $arr;
		}
		
		//选出所有中介（首页）
		function getAllList($type){
			$sql = "select id ,`name` , stars , scores , image , lastlogin , number from ".__table_agentlist__." where level = 0 ".$this->order[$type]." limit ".__index_agent_num__;
			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
					$row["lastlogin"] = $this->getshowtime($row["lastlogin"]);
					$row["stars"] = $this->getStarByScore($row["scores"]);
					$row["number"] = $this->getShowNumber($row["number"]);
					$arr[] = $row;
			   }
			   $result->close();
			}
			//$this->fangDb->disconnect();
			return $arr;
		}

		//根据关键字搜索
		function getSearchList($key , $type , $page){
			$key = $this->safeVal($key);
			$pattern = '/^(0+)/i';
			$replacement = '';
			$key = preg_replace($pattern, $replacement, $key);
			$where = $key!="" ?  " where `name` like '%".$key."%' or number like '%".$key."%' " : "";
			$pt = $this->getSearchPage($key);
			if($page<1){$page=1;}
			if($page>$pt+1){$page = $pt+1;}

			$limit = " limit ".($page-1)*__search_agent_num__." , ".__search_agent_num__;
			$sql = "select id ,`name` , stars , scores , image , number ,lastlogin from ".__table_agentlist__.$where.$this->order[$type].$limit;
			$arr = array();
			if ($result = $this->fangDb->query($sql)) {
			   while ($row = $result->fetch_assoc()) {
					$row["lastlogin"] = $this->getshowtime($row["lastlogin"]);
					$row["stars"] = $this->getStarByScore($row["scores"]);
					$row["number"] = $this->getShowNumber($row["number"]);
				    $arr[] = $row;
			   }
			   $result->close();
			}
			return $arr;
		}
		
		//中介房源在线交流网
		//总共数量
		function getSearchNum($key){
			$key = $this->safeVal($key);
			if($key!=""){$where = " where `name` like '%".$key."%' or number like '%".$key."%'" ;}
			$sql = "select count(*)  from ".__table_agentlist__.$where;
			$result = $this->fangDb->query($sql);
			$row= $result->fetch_array();
			return $row[0];
		}
		
		//页码信息
		function getSearchPage($key){
			$number = $this->getSearchNum($key);
			$page = ceil($number/__search_agent_num__);
			return $page;
		}

		
		//获取中介信息
		function getAgentDetail($aid){
			$aid = $this->safeVal($aid);
			$sql = "select * from ".__table_agentlist__." where id=".$aid." limit 1";
			$result = $this->fangDb->query($sql);
			if($result){
				$row = $result->fetch_assoc();
				$row["stars"] = $this->getStarByScore($row["scores"]);
				$row["number"] = $this->getShowNumber($row["number"]);
				return $row;
			}else{
				return array();
			}
		}
		
		//获取密码
		/*function getAgentPwd(){
			if(__login__==0){return;}
			$table = __user_prop__==0?__table_agentlist__:__table_userlist__;
			$sql = "select password from ".$table." where id=".__user_id__." limit 1";
			$result = $this->fangDb->query($sql);
			if($result){
				$row = $result->fetch_assoc();
				return $row[0];
			}else{
				return array();
			}
		}*/

		//获取个人信息
		function getUserDetail($uid){
			$uid = $this->safeVal($uid);
			$sql = "select * from ".__table_userlist__." where id=".$uid." limit 1";
			$result = $this->fangDb->query($sql);
			if($result){
				$row = $result->fetch_assoc();
				return $row;
			}else{
				return array();
			}
		}

		function getNum(){
			$id = __user_id__;
			$prefix = __user_prop__==0?"a":"u";
			$arr = array();
			$sql1 = "select count(*) from ".__table_sellhouse__." where aid=".$id;
			$result = $this->fangDb->query($sql1);
			$row = $result->fetch_array();
			$arr["shnum"] = $row[0];

			$sql2 = "select count(*) from ".__table_renthouse__." where aid=".$id;
			$result = $this->fangDb->query($sql2);
			$row = $result->fetch_array();
			$arr["rhnum"] = $row[0];

			$sql3 = "select count(*) from ".__table_message__." where receiver='".$prefix."_".$id."' and readflag = 0";
			$result = $this->fangDb->query($sql3);
			$row = $result->fetch_array();
			$arr["msgnum"] = $row[0];
	
			$sql4 = "select count(*) from ".__table_collection__." where userid='".$prefix."_".$id."' and type='s'";
					
			$result = $this->fangDb->query($sql4);
			$row = $result->fetch_array();
			$arr["shcol"] = $row[0];

			$sql5 = "select count(*) from ".__table_collection__." where userid='".$prefix."_".$id."' and type='r'";
			$result = $this->fangDb->query($sql5);
			$row = $result->fetch_array();
			$arr["rhcol"] = $row[0];

			return $arr;
		}

		function getUserNum(){
			$id = __user_id__;
			$arr = array();
			$prefix = __user_prop__==0?"a":"u";
			$sql1 = "select count(*) from ".__table_sellhouse__." where uid=".$id;
			$result = $this->fangDb->query($sql1);
			$row = $result->fetch_array();
			$arr["shnum"] = $row[0];

			$sql2 = "select count(*) from ".__table_renthouse__." where uid=".$id;
			$result = $this->fangDb->query($sql2);
			$row = $result->fetch_array();
			$arr["rhnum"] = $row[0];

			$sql3 = "select count(*) from ".__table_message__." where receiver='u_".$id."' and readflag = 0";
			$result = $this->fangDb->query($sql3);
			$row = $result->fetch_array();
			$arr["msgnum"] = $row[0];

			$sql4 = "select count(*) from ".__table_collection__." where userid='".$prefix."_".$id."' and type='s'";	
			$result = $this->fangDb->query($sql4);
			$row = $result->fetch_array();
			$arr["shcol"] = $row[0];

			$sql5 = "select count(*) from ".__table_collection__." where userid='".$prefix."_".$id."' and type='r'";
			$result = $this->fangDb->query($sql5);
			$row = $result->fetch_array();
			$arr["rhcol"] = $row[0];

			return $arr;
		}

		

		function revalueStar($star , $aid){
			$sql = "select stars , visits , actualstars from ".__table_agentlist__." where id=".$aid." limit 1";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			$ustars = ($row["actualstars"]*$row["visits"]+$star)/($row["visits"]+1);
			$sql = "update ".__table_agentlist__." set visits = visits+1 , stars = ".ceil($ustars)." , actualstars=".$ustars." where id = ".$aid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		function getStarByScore($score){
			$star = ceil($score/200);
			return $star;
		}
		
		function getShowNumber($number){
			$len = strlen($number);
			$n = $number;
			switch($len){
				case 1:
					$n =  "000".$number;
					break;
				case 2:
					$n =  "00".$number;
					break;
				case 3:
					$n =  "0".$number;
					break;
			}
			return $n;
		}


		function getshowtime($time){
			$t = explode(" " , $time);
			//$t0 = explode("-" , $t[0]);
			//$t1= explode(":" , $t[1]);
			return ($t[0]);
		}

	}

	$myAgent = new Agent();
?>